<?php

namespace liketry\controllers;

use Yii;
use common\models\GoodsListRedis;
use \yii\data\Pagination;
use yii\widgets\LinkPager;

/**
 * 商品前端PHP
 * 
 * @author zhouqi
 *        
 */
class GoodslistController extends CustomController {
    /**
     * 商品列表页
     * 
     * @return array
     */
    public function actionLists() {
        // 获取当前页数
        $page = Yii::$app->request->get ( 'page', 1 );
        $return_arr = $this->actionListhtml ( $_REQUEST );
        if ($return_arr ['search_back']) {
            $q_index = GoodsListRedis::getQIndex ( $return_arr ['search_back'] );
            $htmlbrands = $this->actionBackbrands ( $return_arr ['search_back'] );
        } else {
            $q_index = '';
            $htmlbrands = '';
        }
        $return_arr ['page'] = LinkPager::widget ( [ 
                'pagination' => $return_arr ['page'] 
        ] );
        $category_list = \Yii::$app->params ['category_list'];
        return $this->render ( 'lists', [ 
                'list_info' => $return_arr,
                'brands' => $htmlbrands,
                'page' => $page,
                'q_index' => $q_index,
                'category_list' => $category_list 
        ] );
    }
    
    /**
     * 获取商品列表及分页HTML格式数组
     * 
     * @param 当前参数 $request
     *            return array
     */
    public function actionListhtml($request) {
        $return_arr = GoodsListRedis::getListhtml ( $request );
        $html = $this->renderPartial ( 'glist', [ 
                'list' => $return_arr ['arr'] 
        ] );
        $thispage = new Pagination ( [ 
                'totalCount' => $return_arr ['arr_count'],
                'pageSize' => $return_arr ['count'] 
        ] );
        $return_arr = array (
                'text' => $html,
                'page' => $thispage,
                'search_back' => $return_arr ['search_back'] 
        );
        return $return_arr;
    }
    
    /**
     * 根据品牌类型获取品牌HTML
     * 
     * @return Ambigous <NULL, string>
     */
    public function actionGetbrandsbytype() {
        $brandtype = Yii::$app->request->post ( 'brandtype', '' );
        $arr_list = GoodsListRedis::actionSearch ( $_REQUEST );
        if (empty ( $brandtype )) {
            foreach ( $arr_list ['brand_list'] as $k => $v ) {
                foreach ( $v as $k1 => $v1 ) {
                    $brands_info [$v1 ['brand_id']] = $v1 ['brand_name'];
                }
            }
        } else {
            $this_brands = $arr_list ['brand_list'] [$brandtype];
            foreach ( $this_brands as $k => $v ) {
                $brands_info [$v ['brand_id']] = $v ['brand_name'];
            }
        }
        $html = $this->renderPartial ( 'brands', [ 
                'brands' => $brands_info 
        ] );
        return ! empty ( $html ) ? $html : null;
    }
    
    /**
     * 品牌HTML
     */
    public function actionBackbrands($return_brands) {
        $brands_info = [ ];
        foreach ( $return_brands as $k => $v ) {
            foreach ( $v as $k1 => $v1 ) {
                $brands_info [$v1 ['brand_id']] = $v1 ['brand_name'];
            }
        }
        $html = $this->renderPartial ( 'brands', [ 
                'brands' => $brands_info 
        ] );
        return $html;
    }
    
    /**
     * 商品列表页
     * 
     * @return array
     */
    public function actionSearch() {
        // 获取当前页数
        $page = Yii::$app->request->get ( 'page', 1 );
        $return_arr = $this->actionListhtml ( $_REQUEST );
        if ($return_arr ['search_back']) {
            $q_index = GoodsListRedis::getQIndex ( $return_arr ['search_back'] );
            $htmlbrands = $this->actionBackbrands ( $return_arr ['search_back'] );
        } else {
            $q_index = '';
            $htmlbrands = '';
        }
        $return_arr ['page'] = LinkPager::widget ( [ 
                'pagination' => $return_arr ['page'] 
        ] );
        return $this->render ( 'search', [ 
                'list_info' => $return_arr,
                'brands' => $htmlbrands,
                'page' => $page,
                'q_index' => $q_index 
        ] );
    }
}
